﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000367 RID: 871
	internal class ZipLatestObservable<TLeft, TRight, TResult> : OperatorObservableBase<TResult>
	{
		// Token: 0x060011D2 RID: 4562 RVA: 0x00051684 File Offset: 0x0004FA84
		public ZipLatestObservable(IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, TRight, TResult> selector) : base(left.IsRequiredSubscribeOnCurrentThread<TLeft>() || right.IsRequiredSubscribeOnCurrentThread<TRight>())
		{
			this.left = left;
			this.right = right;
			this.selector = selector;
		}

		// Token: 0x060011D3 RID: 4563 RVA: 0x000516B5 File Offset: 0x0004FAB5
		protected override IDisposable SubscribeCore(IObserver<TResult> observer, IDisposable cancel)
		{
			return new ZipLatestObservable<TLeft, TRight, TResult>.ZipLatest(this, observer, cancel).Run();
		}

		// Token: 0x04000A97 RID: 2711
		private readonly IObservable<TLeft> left;

		// Token: 0x04000A98 RID: 2712
		private readonly IObservable<TRight> right;

		// Token: 0x04000A99 RID: 2713
		private readonly Func<TLeft, TRight, TResult> selector;

		// Token: 0x02000368 RID: 872
		private class ZipLatest : OperatorObserverBase<TResult, TResult>
		{
			// Token: 0x060011D4 RID: 4564 RVA: 0x000516C4 File Offset: 0x0004FAC4
			public ZipLatest(ZipLatestObservable<TLeft, TRight, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x060011D5 RID: 4565 RVA: 0x0005170C File Offset: 0x0004FB0C
			public IDisposable Run()
			{
				IDisposable disposable = this.parent.left.Subscribe(new ZipLatestObservable<TLeft, TRight, TResult>.ZipLatest.LeftObserver(this));
				IDisposable disposable2 = this.parent.right.Subscribe(new ZipLatestObservable<TLeft, TRight, TResult>.ZipLatest.RightObserver(this));
				return StableCompositeDisposable.Create(disposable, disposable2);
			}

			// Token: 0x060011D6 RID: 4566 RVA: 0x00051750 File Offset: 0x0004FB50
			public void Publish()
			{
				if (!this.leftCompleted || this.leftStarted)
				{
					if (!this.rightCompleted || this.rightStarted)
					{
						if (!this.leftStarted || !this.rightStarted)
						{
							return;
						}
						TResult value;
						try
						{
							value = this.parent.selector(this.leftValue, this.rightValue);
						}
						catch (Exception error)
						{
							try
							{
								this.observer.OnError(error);
							}
							finally
							{
								base.Dispose();
							}
							return;
						}
						this.OnNext(value);
						this.leftStarted = false;
						this.rightStarted = false;
						if (!this.leftCompleted)
						{
							if (!this.rightCompleted)
							{
								return;
							}
						}
						try
						{
							this.observer.OnCompleted();
						}
						finally
						{
							base.Dispose();
						}
						return;
					}
				}
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x060011D7 RID: 4567 RVA: 0x00051878 File Offset: 0x0004FC78
			public override void OnNext(TResult value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x060011D8 RID: 4568 RVA: 0x00051888 File Offset: 0x0004FC88
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x060011D9 RID: 4569 RVA: 0x000518C0 File Offset: 0x0004FCC0
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000A9A RID: 2714
			private readonly ZipLatestObservable<TLeft, TRight, TResult> parent;

			// Token: 0x04000A9B RID: 2715
			private readonly object gate = new object();

			// Token: 0x04000A9C RID: 2716
			private TLeft leftValue = default(TLeft);

			// Token: 0x04000A9D RID: 2717
			private bool leftStarted;

			// Token: 0x04000A9E RID: 2718
			private bool leftCompleted;

			// Token: 0x04000A9F RID: 2719
			private TRight rightValue = default(TRight);

			// Token: 0x04000AA0 RID: 2720
			private bool rightStarted;

			// Token: 0x04000AA1 RID: 2721
			private bool rightCompleted;

			// Token: 0x02000369 RID: 873
			private class LeftObserver : IObserver<TLeft>
			{
				// Token: 0x060011DA RID: 4570 RVA: 0x000518F8 File Offset: 0x0004FCF8
				public LeftObserver(ZipLatestObservable<TLeft, TRight, TResult>.ZipLatest parent)
				{
					this.parent = parent;
				}

				// Token: 0x060011DB RID: 4571 RVA: 0x00051908 File Offset: 0x0004FD08
				public void OnNext(TLeft value)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.leftStarted = true;
						this.parent.leftValue = value;
						this.parent.Publish();
					}
				}

				// Token: 0x060011DC RID: 4572 RVA: 0x00051968 File Offset: 0x0004FD68
				public void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.OnError(error);
					}
				}

				// Token: 0x060011DD RID: 4573 RVA: 0x000519B0 File Offset: 0x0004FDB0
				public void OnCompleted()
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.leftCompleted = true;
						if (this.parent.rightCompleted)
						{
							this.parent.OnCompleted();
						}
					}
				}

				// Token: 0x04000AA2 RID: 2722
				private readonly ZipLatestObservable<TLeft, TRight, TResult>.ZipLatest parent;
			}

			// Token: 0x0200036A RID: 874
			private class RightObserver : IObserver<TRight>
			{
				// Token: 0x060011DE RID: 4574 RVA: 0x00051A14 File Offset: 0x0004FE14
				public RightObserver(ZipLatestObservable<TLeft, TRight, TResult>.ZipLatest parent)
				{
					this.parent = parent;
				}

				// Token: 0x060011DF RID: 4575 RVA: 0x00051A24 File Offset: 0x0004FE24
				public void OnNext(TRight value)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.rightStarted = true;
						this.parent.rightValue = value;
						this.parent.Publish();
					}
				}

				// Token: 0x060011E0 RID: 4576 RVA: 0x00051A84 File Offset: 0x0004FE84
				public void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.OnError(error);
					}
				}

				// Token: 0x060011E1 RID: 4577 RVA: 0x00051ACC File Offset: 0x0004FECC
				public void OnCompleted()
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.rightCompleted = true;
						if (this.parent.leftCompleted)
						{
							this.parent.OnCompleted();
						}
					}
				}

				// Token: 0x04000AA3 RID: 2723
				private readonly ZipLatestObservable<TLeft, TRight, TResult>.ZipLatest parent;
			}
		}
	}
}
